package cn.doctorpda.conference.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.adapter.InfoAdapter;
import cn.doctorpda.conference.view.DividerItemDecoration;
import cn.doctorpda.conference.view.recyclerview.XRecylcerView;

/**
 * Created by huangjinlong on 2016/5/23.
 */
public abstract class BaseListFragment extends BaseFragment{

    public abstract String getTitle();
    public abstract RecyclerView.Adapter getAdapter();
    public abstract Fragment getFragment();
    public SwipeRefreshLayout swipeRefreshLayout;
    public XRecylcerView recyclerView;
    private Toolbar toolbar;
    public LinearLayout ll_no_data;
    public TextView tv_no_data;
    public ImageView imv_no_data;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.common_list_base, container, false);
        initView(view);

        return view;
    }

    protected void initView(View view){
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.info_refresh);
        recyclerView = (XRecylcerView) view.findViewById(R.id.info_loadmore);
        toolbar = (Toolbar) view.findViewById(R.id.title_common);
        ((BaseActivity)getActivity()).setSupportActionBar(toolbar);
        getActivity().setTitle(getTitle());
        ActionBar actionBar = ((BaseActivity)getActivity()).getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),
                DividerItemDecoration.HORIZONTAL_LIST));
        recyclerView.setAdapter(getAdapter());
        recyclerView.setLoadingListener((XRecylcerView.LoadingListener) getFragment());
        swipeRefreshLayout.setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) getFragment());

        ll_no_data = (LinearLayout)view.findViewById(R.id.ll_no_data);
        tv_no_data = (TextView) view.findViewById(R.id.tv_no_data_show);
        imv_no_data = (ImageView) view.findViewById(R.id.imv_no_data_show);

    }

    @Override
    public String getPageName() {
        return "BaseListFragment";
    }
}
